package com.example.yuanshen;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnqingkong;
    private Button btnjia, btnjian, btnchen, btnchu, btndengyu;
    private TextView tvShow;

    int num1 = 0, num0 = 0, num2 = 0, result, flag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);

        btn0 = findViewById(R.id.btn0);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);
        btn5 = findViewById(R.id.btn5);
        btn6 = findViewById(R.id.btn6);
        btn7 = findViewById(R.id.btn7);
        btn8 = findViewById(R.id.btn8);
        btn9 = findViewById(R.id.btn9);
        btnjia = findViewById(R.id.btnjia);
        btnjian = findViewById(R.id.btnjian);
        btnchen = findViewById(R.id.btnchen);
        btnchu = findViewById(R.id.btnchu);
        btndengyu = findViewById(R.id.btndengyu);
        btnqingkong = findViewById(R.id.btnqingkong);

        tvShow = findViewById(R.id.tvShow);

        btn0.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btnjia.setOnClickListener(this);
        btnjian.setOnClickListener(this);
        btnchen.setOnClickListener(this);
        btnchu.setOnClickListener(this);
        btndengyu.setOnClickListener(this);
        btnqingkong.setOnClickListener(this);

        /*btn0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvShow.setText("0");
                Toast.makeText(MainActivity.this, "原神 启动！", Toast.LENGTH_SHORT).show();
            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvShow.setText("1");
                Toast.makeText(MainActivity.this, "原神 启动！", Toast.LENGTH_SHORT).show();
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvShow.setText("2");
                Toast.makeText(MainActivity.this, "原神 启动！", Toast.LENGTH_SHORT).show();
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvShow.setText("3");
                Toast.makeText(MainActivity.this, "原神 启动！", Toast.LENGTH_SHORT).show();
            }
        });
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvShow.setText("4");
                Toast.makeText(MainActivity.this, "原神 启动！", Toast.LENGTH_SHORT).show();
            }
        });
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvShow.setText("5");
                Toast.makeText(MainActivity.this, "原神 启动！", Toast.LENGTH_SHORT).show();
            }
        });
        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvShow.setText("6");
                Toast.makeText(MainActivity.this, "原神 启动！", Toast.LENGTH_SHORT).show();
            }
        });
        btn7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvShow.setText("7");
                Toast.makeText(MainActivity.this, "原神 启动！", Toast.LENGTH_SHORT).show();
            }
        });
        btn8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvShow.setText("8");
                Toast.makeText(MainActivity.this, "原神 启动！", Toast.LENGTH_SHORT).show();
            }
        });
        btn9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvShow.setText("9");
                Toast.makeText(MainActivity.this, "原神 启动！", Toast.LENGTH_SHORT).show();
            }
        });*/
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn0:
                tvShow.setText(tvShow.getText() + "0");
                break;
            case R.id.btn1:
                tvShow.setText(tvShow.getText() + "1");
                break;
            case R.id.btn2:
                tvShow.setText(tvShow.getText() + "2");
                break;
            case R.id.btn3:
                tvShow.setText(tvShow.getText() + "3");
                break;
            case R.id.btn4:
                tvShow.setText(tvShow.getText() + "4");
                break;
            case R.id.btn5:
                tvShow.setText(tvShow.getText() + "5");
                break;
            case R.id.btn6:
                tvShow.setText(tvShow.getText() + "6");
                break;
            case R.id.btn8:
                tvShow.setText(tvShow.getText() + "8");
                break;
            case R.id.btn9:
                tvShow.setText(tvShow.getText() + "9");
                break;
            case R.id.btn7:
                tvShow.setText(tvShow.getText() + "7");
                break;
            case R.id.btnjia:
                tvShow.setText(tvShow.getText() + "+");
                break;
            case R.id.btnchen:
                tvShow.setText(tvShow.getText() + "*");
                break;
            case R.id.btnjian:
                tvShow.setText(tvShow.getText() + "-");
                break;
            case R.id.btnchu:

                tvShow.setText(tvShow.getText() + "/");
                break;
            case R.id.btndengyu:
                tvShow.setText(tvShow.getText()+"=");
                if (tvShow.getText().toString().contains("+")) {
                    num1 = Integer.valueOf(tvShow.getText().toString().substring(0, tvShow.getText().toString().indexOf("+")));
                    num2 = Integer.valueOf(tvShow.getText().toString().substring(tvShow.getText().toString().indexOf("+") + 1, tvShow.getText().toString().indexOf("=")));
                    result = num1 + num2;
                    tvShow.setText(tvShow.getText().toString() + result);
                } else if (tvShow.getText().toString().contains("-")) {
                    num1 = Integer.valueOf(tvShow.getText().toString().substring(0, tvShow.getText().toString().indexOf("-")));
                    num2 = Integer.valueOf(tvShow.getText().toString().substring(tvShow.getText().toString().indexOf("-") + 1, tvShow.getText().toString().indexOf("=")));
                    result = num1 - num2;
                    tvShow.setText(tvShow.getText().toString() + result);
                } else if (tvShow.getText().toString().contains("*")) {
                    num1 = Integer.valueOf(tvShow.getText().toString().substring(0, tvShow.getText().toString().indexOf("*")));
                    num2 = Integer.valueOf(tvShow.getText().toString().substring(tvShow.getText().toString().indexOf("*") + 1, tvShow.getText().toString().indexOf("=")));
                    result = num1 * num2;
                    tvShow.setText(tvShow.getText().toString() + result);
                } else if (tvShow.getText().toString().contains("/")) {
                    num1 = Integer.valueOf(tvShow.getText().toString().substring(0, tvShow.getText().toString().indexOf("/")));
                    num2 = Integer.valueOf(tvShow.getText().toString().substring(tvShow.getText().toString().indexOf("/") + 1, tvShow.getText().toString().indexOf("=")));
                    result = num1 / num2;
                    tvShow.setText(tvShow.getText().toString() + result);
                }

                    break;
            case R.id.btnqingkong:
                tvShow.setText("");
                break;
        }

    }
}